<template>
  <div>
    <!-- 提交初审,可选择有待初审权限的用户，crm:project:firstTrial -->
    <Dialog
      ref="submitDialogRef"
      :btn1="'确认'"
      :btn2="'取消'"
      summary="请确认是否将项目提交初审"
      tip="接收人请选择项目总监"
      title="项目订单通过确认"
      @handelOK="submitExamineHttp" />
    <!-- 初审，通过或退回,可选择有待复审权限的人员，crm:project:recheck  -->
    <Dialog
      ref="firstDialogRef"
      :btn1="'确认'"
      :btn2="'取消'"
      :title="examineTitle"
      :summary="examineSummary"
      tip="接收人请选择指定财务人员"
      @handelOK="firstExamineHttp" />
    <!-- 复审，通过或退回,可选择有待复审权限的人员，crm:project:recheck  -->
    <Dialog
      ref="recheckDialogRef"
      :btn1="'确认'"
      :btn2="'取消'"
      :title="examineTitle"
      :summary="examineSummary"
      tip="接收人请选择部门负责人"
      @handelOK="recheckExamineHttp" />
    <!-- 待确认  -->
    <Dialog
      ref="confirmDialogRef"
      :btn1="'确认'"
      :btn2="'取消'"
      :title="examineTitle"
      :summary="examineSummary"
      tip="接收人请选择指定财务人员"
      @handelOK="confirmHttp" />
    <!-- 退回  -->
    <Dialog
      ref="backDialogRef"
      :btn1="'确认'"
      :btn2="'取消'"
      summary="请选择要退回的流程节点"
      title="项目订单退回确认"
      @handelOK="backHttp" />
  </div>
</template>

<script>
import ElMessage from '@/utils/message';
// api
import { submitExamine, firstExamine, secondExamine, confirm, projectBack, projectSeal } from '@/api/projectDetail';
// components
import Dialog from './footBtnDialog';

export default {
  components: { Dialog },
  data() {
    return {
      examineTitle: '',
      examineSummary: ''
    };
  },
  methods: {
    // 待提交-提交初审
    submitExamine({ extendParams, defaultObjectUser, projectId }) {
      const loadUserParams = { id: projectId, authUrl: 'crm:project:firstTrial' }; // 可选择有待初审权限的用户，authUrl:crm:project:firstTrial
      const params = {
        defaultMsg: '亲~你有待审批的项目订单',
        extendParams: extendParams,
        loadUserParams,
        notClearUser: true
      };
      if (defaultObjectUser) params.defaultObjectUser = defaultObjectUser;
      this.$refs.submitDialogRef.showDialog(params);
    },
    submitExamineHttp(data) {
      data.status = 1;
      if (data && data.users) { // 通过则缓存
        localStorage.setItem('submitExamineHttp', JSON.stringify(data.users));// 缓存每一步骤的人员
        delete data.users;
      }
      submitExamine(data).then(res => {
        this.callback(res, '提交成功');
      });
    },
    // 待初审-通过/退回
    firstExamine({ extendParams, defaultObjectUser, projectId }) {
      let defaultMsg;
      const loadUserParams = {
        id: projectId
      }; // 通过时可选择有待复审权限的用户，退回时不限制但默认选中创建人
      if (extendParams.status === 1) {
        this.examineTitle = '项目订单通过确认';
        this.examineSummary = '请确认是否将项目提交财务复审';
        defaultMsg = '亲~你有待审批的项目订单';
        loadUserParams.perPage = 15;
        loadUserParams.authUrl = 'crm:project:recheck';
      } else {
        this.examineTitle = '项目订单退回确认';
        this.examineSummary = '请确认是否将项目退回上一步';
        defaultMsg = '亲~你有被退回的项目订单';
        loadUserParams.perPage = 15;
        delete loadUserParams.authUrl;
      }
      this.$refs.firstDialogRef.showDialog({ defaultMsg, loadUserParams, extendParams, defaultObjectUser });
    },
    firstExamineHttp(data) {
      if (data && data.users) { // 通过则缓存
        localStorage.setItem('firstExamineUsers', JSON.stringify(data.users));// 缓存每一步骤的人员
        delete data.users;
      }
      firstExamine(data).then(res => {
        this.callback(res);
      });
    },
    // 待复审-通过/退回
    recheckExamine({ extendParams, defaultObjectUser, projectId, isMediumSale }) {
      let defaultMsg;
      const loadUserParams = { id: projectId }; // 通过时可选择有待确认权限的用户，退回时不限制
      if (extendParams.status === 1) {
        this.examineTitle = '项目订单通过确认';
        this.examineSummary = '请确认是否通过审批';
        defaultMsg = '亲~你有待确认的项目订单';
        loadUserParams.perPage = 15;
        loadUserParams.authUrl = 'crm:project:confirm';
        if (isMediumSale) {
          defaultMsg = '亲~你有待封存的项目订单';
          loadUserParams.authUrl = 'crm:project:seal';
        }
      } else {
        this.examineTitle = '项目订单退回确认';
        this.examineSummary = '请确认是否将项目退回上一步';
        defaultMsg = '亲~你有被退回的项目订单';
        loadUserParams.perPage = 15;
        delete loadUserParams.authUrl;
      }
      this.$refs.recheckDialogRef.showDialog({ defaultMsg, extendParams, defaultObjectUser, loadUserParams });
    },
    recheckExamineHttp(data) {
      if (data && data.users) { // 通过则缓存
        localStorage.setItem('recheckExamineHttp', JSON.stringify(data.users));// 缓存每一步骤的人员
        delete data.users;
      }
      secondExamine(data).then(res => {
        this.callback(res);
      });
    },
    // 待确认
    confirm({ extendParams, defaultObjectUser, projectId }) {
      let defaultMsg;
      const loadUserParams = { id: projectId };
      if (extendParams.status === 1) {
        this.examineTitle = '项目订单通过确认';
        this.examineSummary = '请确认是否通过审批';
        defaultMsg = '亲~你有待封存的项目订单';
        loadUserParams.authUrl = 'crm:project:seal';
        loadUserParams.perPage = 15;
      } else {
        this.examineTitle = '项目订单拒绝确认';
        this.examineSummary = '请确认是否将项目退回上一步';
        defaultMsg = '亲~你有被退回的项目订单';
      }
      this.$refs.confirmDialogRef.showDialog({ defaultMsg, extendParams, defaultObjectUser, loadUserParams });
    },
    confirmHttp(data) {
      if (data && data.users) { // 通过则缓存
        localStorage.setItem('confirmHttp', JSON.stringify(data.users));// 缓存每一步骤的人员
        delete data.users;
      }
      confirm(data).then(res => {
        this.callback(res);
      });
    },
    // 退回
    projectBack({ extendParams, defaultObjectUser, projectId }) {
      const loadUserParams = { id: projectId };
      this.$refs.backDialogRef.showDialog({ extendParams, defaultMsg: '亲~你有被退回的项目订单', defaultObjectUser, loadUserParams });
    },
    backHttp(data) {
      projectBack(data).then(res => {
        this.callback(res);
      });
    },
    // 封存
    projectSeal(data) {
      this.$confirm('请确认是否封存项目订单', '', {
        confirmButtonText: '确定',
        type: 'warning',
        showClose: false,
        center: true,
        iconClass: 'charge-icon',
        dangerouslyUseHTMLString: true,
        customClass: 'del-message del-message-cancel'
      }).then(() => {
        data.status = 1;
        projectSeal(data).then(res => {
          this.callback(res);
          this.$emit('cancelSealCallBack');
        });
      });
    },
    cancelSeal(data) {
      this.$confirm('请确认是否取消封存', '', {
        confirmButtonText: '确定',
        type: 'warning',
        showClose: false,
        center: true,
        iconClass: 'charge-icon',
        dangerouslyUseHTMLString: true,
        customClass: 'del-message del-message-cancel'
      }).then(() => {
        data.isSendMsg = false;
        data.status = 4;
        data.currentStatus = 5;
        projectBack(data).then(res => {
          this.callback(res);
          this.$emit('cancelSealCallBack');
        });
      });
    },
    // tool
    callback(res, msg) {
      if (res.code === 200) {
        ElMessage.success(msg || '操作成功');
        this.$emit('getInfo');
      } else {
        ElMessage.success(res.message);
      }
    }
  }
};
</script>

<style>

</style>
